package com.lenovo.nova.util.os.storage;

import com.lenovo.nova.util.StringUtil;
import com.lenovo.nova.util.debug.slog;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Environment;
import android.os.StatFs;
import android.sax.StartElementListener;

@SuppressLint("NewApi")
public class StorageUtil {

	private static final String TAG = StorageUtil.class.getSimpleName();
	/**
	 * 获取外部存储设备总的大小
	 * Environment.getExternalStorageDirectory()
	 * @return
	 */
	public long getExternalStorageSize(){
		long result = 0;
		String path = Environment.getExternalStorageDirectory().getAbsolutePath();
		slog.p(TAG,"getExternalStorageSize path is " + path );
		StatFs statfs = new StatFs(path);
		try {
			result = statfs.getBlockCountLong() * statfs.getBlockSizeLong();
		} catch (NoSuchMethodError e) {
			result = (long)statfs.getBlockCount() * (long)statfs.getBlockSize();
		}
		return result;
	}
	
	public long getExternalAvaliableStorageSize(){
		long result = 0;
		String path = Environment.getExternalStorageDirectory().getAbsolutePath();
		slog.p(TAG,"getExternalStorageSize path is " + path );
		StatFs statfs = new StatFs(path);
		try {
			result = statfs.getAvailableBlocksLong() * statfs.getBlockSizeLong();
		} catch (NoSuchMethodError e) {
			result = (long)statfs.getAvailableBlocks() * (long)statfs.getBlockSize();
		}
		return result;
	}
	
	public String format(Context context,long size){
		return StringUtil.formatShortSize(context, getExternalStorageSize());
	}
	
}
